<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');

class Billprogress extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');
		$this->load->library('PHPexcel');

	}

	public function index(){
	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'));
	  $this->load->view('layouts/footer');
	 }

	 public function add_form(){
		$this->load->view($this->session->userdata('foradd'));
	}

	public function edit_form($id){
		$this->load->view($this->session->userdata('foredit'));
	}

	public function view_form($id){
		$this->load->view($this->session->userdata('forview'));
	}

	public function get_status() {
		//$date = date($this->input->post('year').'-'.$this->input->post('month'));
		//var_dump($this->input->post('year').'-'.$this->input->post('month'));
		echo json_encode($this->billprogress_model->get_status($this->input->post('year').'-'.$this->input->post('month')));
	}

	public function get_pend(){
		$sure = $this->billprogress_model->get_pnd($_GET['date'],$_GET['zone'],$_GET['zone2']);
		$this->load->view($this->session->userdata('foradd'),array(
			'title' => 'Pending List',
			'datas'	=> $sure
			));
		//echo json_encode($this->billprogress_model->get_pnd($_GET['date'],$_GET['zone']));
		//var_dump($this->billprogress_model->get_pnd($_GET['date'],$_GET['zone']));
	}

	public function gen_excel() {
		$year = 'null';
		$month = 'null';
		if (!empty($_GET['year'])) {
			$year = $_GET['year'];
		}
		if (!empty($_GET['month'])) {
			$month = $_GET['month'];
		}
		//activate worksheet number 1
		$excel = new PHPExcel();
		$excel->setActiveSheetIndex(0);
		//name the worksheet
		$excel->getActiveSheet()->setTitle('Collection Details Report');
		//set cell A1 content with some text
		$excel->getActiveSheet()->setCellValue('A1', 'Billing Progress ' . $year . '-' . $month);
		$excel->getActiveSheet()->setCellValue('A3', 'Zone 1');
		$excel->getActiveSheet()->setCellValue('B3', 'Zone 2');
		$excel->getActiveSheet()->setCellValue('C3', 'Zone 3');
		$excel->getActiveSheet()->setCellValue('D3', 'Zone 4');
		$excel->getActiveSheet()->setCellValue('E3', 'Zone 5');
		$excel->getActiveSheet()->setCellValue('F3', 'Percent Completed');
		$excel->getActiveSheet()->setCellValue('G3', 'Pending Count');
		$excel->getActiveSheet()->setCellValue('H3', 'Customer Count');
		$excel->getActiveSheet()->setCellValue('I3', 'Bill Count');
		$excel->getActiveSheet()->setCellValue('J3', 'Bill Amount');
		$result = $this->billprogress_model->get_status($_GET['year'].'-'.$_GET['month']);
		$cellnum = 4;
		$t_bill_amt = 0;
		$t_bill_count = 0;
		$t_custs_count = 0;
		$t_pending_count = 0;
		$t_percent_completed = 0;
		foreach ($result as $key) {
			$excel->getActiveSheet()->setCellValue('A' . $cellnum, $key->zone_1);
			$excel->getActiveSheet()->setCellValue('B' . $cellnum, $key->zone_2);
			$excel->getActiveSheet()->setCellValue('C' . $cellnum, $key->zone_3);
			$excel->getActiveSheet()->setCellValue('D' . $cellnum, $key->zone_4);
			$excel->getActiveSheet()->setCellValue('E' . $cellnum, $key->zone_5);
			$excel->getActiveSheet()->setCellValue('F' . $cellnum, $key->percent_completed);
			$excel->getActiveSheet()->setCellValue('G' . $cellnum, $key->pending_count);
			$excel->getActiveSheet()->setCellValue('H' . $cellnum, $key->custs_count);
			$excel->getActiveSheet()->setCellValue('I' . $cellnum, $key->bill_count);
			$excel->getActiveSheet()->setCellValue('J' . $cellnum, $key->bill_amt);
			$excel->getActiveSheet()->getStyle('J' . $cellnum)->getNumberFormat()->setFormatCode('###,###,###.00');
			//$excel->getActiveSheet()->getStyle('F'.$cellnum)->getNumberFormat()->applyFromArray(array($key->percent_completed => PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00));
			$t_bill_amt = $t_bill_amt + $key->bill_amt;
			$t_bill_count = $t_bill_count  + $key->bill_count;
			$t_custs_count = $t_custs_count + $key->custs_count;
			$t_pending_count = $t_pending_count + $key->pending_count;
			$cellnum++;
		}
		$t_percent_completed = ($t_bill_count * 100) / $t_custs_count;
		$excel->getActiveSheet()->setCellValue('F' . $cellnum, $t_percent_completed);
		$excel->getActiveSheet()->setCellValue('G' . $cellnum, $t_pending_count);
		$excel->getActiveSheet()->setCellValue('H' . $cellnum, $t_custs_count);
		$excel->getActiveSheet()->setCellValue('I' . $cellnum, $t_bill_count);
		$excel->getActiveSheet()->setCellValue('J' . $cellnum, $t_bill_amt);
		$excel->getActiveSheet()->getStyle('J' . $cellnum)->getNumberFormat()->setFormatCode('###,###,###.00');
		$excel->getActiveSheet()->getStyle('F'.$cellnum.':J'.$cellnum)->getFont()->setBold(true);

		//$excel->getActiveSheet()->getStyle('A1:F' . $cellnum = $cellnum - 1)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

		// $excel->getActiveSheet()->getStyle('A1:F1')->getFill()->setFillType(PHPexcel_Style_Fill::FILL_SOLID);
		//       $excel->getActiveSheet()->getStyle('A1:F1')->getFill()->getStartColor()->setARGB('29bb04');
		//       // Add some data
		//       $excel->getActiveSheet()->getStyle("A1:F1")->getFont()->setBold(true);
		//       $excel->getActiveSheet()->getStyle('A1:F1')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getFill()->setFillType(PHPexcel_Style_Fill::FILL_SOLID);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getFill()->getStartColor()->setARGB('6F6F6F');
		// Add some data
		//$objPHPExcel->getActiveSheet()->getStyle('A1')->getNumberFormat()->applyFromArray(array('code' => PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00));
		$excel->getActiveSheet()->getStyle("A3:J3")->getFont()->setBold(true);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$excel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('C')->setAutoSize(false);
		$excel->getActiveSheet()->getColumnDimension('C')->setWidth(40);
		$excel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
		$excel->getActiveSheet()->getRowDimension('1')->setRowHeight(25);
		//change the font size
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(12);
		//make the font become bold
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//merge cell A1 until D1
		$excel->getActiveSheet()->mergeCells('A1:J1');
		//set aligment to center for that merged cell (A1 to D1)
		$excel->getActiveSheet()->getStyle('A3:J3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		//$excel->getActiveSheet()->getStyle("A2:F2")->getFont()->getColor()->setRGB('FFFFFF');

		$excel->getActiveSheet()->setShowGridlines(false);

		$filename = 'Collection_Details_Report_ ' . date('Ymd_his') . '.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
		$objWriter->save('php://output');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("assets/tmp/" . $filename);
		//$this->index();
		// $data = ob_get_contents();
		//$tester =  $objWriter->save($filename);
		//force_download($filename,$objWriter->save('php://output')));
	}

	public function gen_pdf() {
		$date = $_GET['year'].'-'.$_GET['month'];
		$result = $this->billprogress_model->get_status($date);
		//var_dump($result);
		$table_data = '';
		//<td style='border-bottom:1px solid; text-align: center;width=8%; padding: 5px;'>Year</td>
		//<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->year . '</td>
		foreach ($result as $key) {
			$table_data .= '<tr><td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;">' . $key->zone_1 . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . round($key->percent_completed,2) . ' %</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->pending_count . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="center">' . $key->custs_count . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->bill_count . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->bill_amt . '</td></tr>';
		}
		$content = "
		<page>
        	<table style='width: 100%; padding-top:-10px;'>
	            <tr>
			        <td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '></td>
			        <td style='text-align: center;width=33%; padding-top:-5px;'>
						<h4>Bill Year-Month " . $_GET['year'] . "-" . $_GET['month'] . "</h4>
					</td>
					<td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '>     </td>
				</tr>
			</table><br><br>
			<table cellspacing='0' style='width: 100%; position: relative;'>
				<tr>
					<td style='border-bottom:1px solid; text-align: center;width=18%; padding: 5px;'>Zone 1</td>
					<td style='border-bottom:1px solid; text-align: center;width=16%; padding: 5px;'>Percent Completed</td>
					<td style='border-bottom:1px solid; text-align: center;width=16%; padding: 5px;'>Pending Count</td>
					<td style='border-bottom:1px solid; text-align: center;width=16%; padding: 5px;'>Customer Count</td>
					<td style='border-bottom:1px solid; text-align: center;width=16%; padding: 5px;'>Bill Count</td>
					<td style='border-bottom:1px solid; text-align: center;width=18%; padding: 5px;'>Bill Amount</td>
				</tr>" .
			$table_data
			. "</table>
		</page>
		";
		require_once dirname(__FILE__) . '\..\..\..\assets\html2pdf\html2pdf.class.php';
		$html2pdf = new HTML2PDF('P', 'A4', 'en');
		$html2pdf->pdf->SetTitle('Collection Details Report');
		$html2pdf->WriteHTML($content);
		$html2pdf->Output('Collection_Details_' . date("dmY_His") . '.pdf', 'D');
	}

}